using System;
using SqlSugar;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model;
using ZR.Model.Dto;
using ZR.Model.Business;
using ZR.Repository;
using ZR.Service.Business.IBusinessService;
using System.Linq;
using Infrastructure.Model;
using Microsoft.Extensions.Options;
using ZR.Infrastructure.ANet;
using Newtonsoft.Json.Linq;
using Infrastructure;

namespace ZR.Service.Business
{
    /// <summary>
    /// 土地出让公告信息Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IV3TdsyqDealBehaviorInfoService), ServiceLifetime = LifeTime.Transient)]
    public class V3TdsyqDealBehaviorInfoService : BaseService<V3TdsyqDealBehaviorInfo>, IV3TdsyqDealBehaviorInfoService
    {
        private OptionsSetting OptionsSetting;

        public V3TdsyqDealBehaviorInfoService(IOptions<OptionsSetting> options)
        {
            this.OptionsSetting = options.Value;
        }


        /// <summary>
        /// 查询土地出让公告信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<V3TdsyqDealBehaviorInfoDto> GetList(V3TdsyqDealBehaviorInfoQueryDto parm)
        {
            var predicate = Expressionable.Create<V3TdsyqDealBehaviorInfo>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.BidArea), it => it.BidArea == parm.BidArea);
            predicate = predicate.AndIF(parm.AnId != null, it => it.AnId == parm.AnId);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.DataKey), it => it.DataKey == parm.DataKey);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PushStatus), it => it.PushStatus == parm.PushStatus);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.AnnouncementTitle), it => it.AnnouncementTitle.Contains(parm.AnnouncementTitle));
            //predicate = predicate.AndIF(parm.BeginPublishingTime == null, it => it.PublishingTime >= DateTime.Now.ToShortDateString().ParseToDateTime());
            predicate = predicate.AndIF(parm.BeginPublishingTime != null, it => it.PublishingTime >= parm.BeginPublishingTime);
            predicate = predicate.AndIF(parm.EndPublishingTime != null, it => it.PublishingTime <= parm.EndPublishingTime);
            var response = Queryable()
                //.OrderBy("PublishingTime desc")
                .Where(predicate.ToExpression())
                .ToPage<V3TdsyqDealBehaviorInfo, V3TdsyqDealBehaviorInfoDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="TdcrggId"></param>
        /// <returns></returns>
        public V3TdsyqDealBehaviorInfo GetInfo(long TdcrggId)
        {
            var response = Queryable()
                .Where(x => x.TdcrggId == TdcrggId)
                .First();

            return response;
        }

        /// <summary>
        /// 添加土地出让公告信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public V3TdsyqDealBehaviorInfo AddV3TdsyqDealBehaviorInfo(V3TdsyqDealBehaviorInfo model)
        {
            return Context.Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改土地出让公告信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateV3TdsyqDealBehaviorInfo(V3TdsyqDealBehaviorInfo model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 土地出让公告粤公平主题库新增推送
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string PushAddTheme(long id)
        {
            var behavior = Queryable()
               .Where(x => x.TdcrggId == id)
               .First();
            string dataJson = "{\"ROOT\":{\"HEADDATA\":{\"FROMPLATFORMCODE\":\"124413006905190362\",\"TOPLATFORMCODE\":\"12440000079518100E\",\"DATA_NO\":\"3B41\",\"DATA_EN\":\"v3_gycq_trade_notice\",\"DATA_CN\":\"v3.0_国有产权_11.1.1 交易公告信息\",\"DATA_TYPE\":\"2\",\"DATA_KEY\":\"ca5efc7b26514e04aa9735b486e54011\",\"TIMESTAMP\":\"202309131083045\",\"VERSION\":\"3.0\"},\"DESCONTEXT\":{\"PROJECT_CODE\":\"CQ441301001001\",\"PROJECT_NAME\":\"惠州市江北云山西路德威1号商铺\",\"NOTICE_CODE\":\"惠公易产市直[2023]080号\",\"NOTICE_TITAL\":\"惠州市江北云山西路德威1号商铺出租挂牌公告\",\"BID_CODE\":\"GJB2023-080,GJB2023-081\",\"OBJECT_NAME\":\"惠州市江北云山西路德威1号商铺出租挂牌公告,惠州市江北云山西路德威2号商铺出租挂牌公告\",\"OBJECT_AMOUNT\":\"2\",\"TRA_PLACE_CODE\":\"441301\",\"REGION_CODE\":\"441301\",\"PROJECT_CLASSIFY_CODE\":\"C01\",\"CN_PROJECT_TYPE\":\"C01\",\"NOTICE_START_TIME\":\"20230913072000\",\"ANNOUNCEMENT_TYPE\":\"1\",\"NOTICE_NATURE\":\"1\",\"NOTICE_MEDIA\":\"惠州市公共资源交易中心产权交易网上竞价系统\",\"URL\":\"https://cqjy.huizhou.gov.cn/Web/Index/LandDetail?id=18118\",\"ANNOUNCEMENT_CONNECT\":\"<p>公告内容UPDATE2.0</p>\",\"NOTICE_TERM\":\"<p>公告条件说明</p>\",\"NOTICE_END_TIME\":\"20230915083000\",\"REGISTRATION_URL\":\"https://cqjy.huizhou.gov.cn/Client/ClientLogin/Login\",\"UNIFIED_DEAL_CODE\":\"C01-124413006905190362-20230912-001001-5\",\"PLATFORM_CODE\":\"124413006905190362\",\"PUB_SERVICE_PLAT_CODE\":\"124413006905190362\",\"DATA_TIMESTAMP\":\"20230913083045\"},\"DESBINARYS\":{}}}";

            string result = HttpHelper.ThemeHttpPost(dataJson);
            return result;
        }

    }
}